package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.mbc.common.entities.Company;
import com.mbc.common.data.CompanyDataList;
import com.mbc.common.param.CriteriaParams;

public class CompanyDAOImpls extends DAO<Company> implements CompanyDAO {

	public CompanyDAOImpls(Connection conn)
	{
		this.conn = conn;
		this.table_name = "Company";
	}

	@Override
	public void create(Company entity) throws SQLException {
		query = "insert into " + table_name + " ( Company_Id, Code, Name, Myanmar_Name) values( ?, ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
				pstmt.setLong(i++, entity.getCompany_Id());
		pstmt.setString(i++, entity.getCode());
		pstmt.setString(i++, entity.getName());
		pstmt.setString(i++, entity.getMyanmar_Name());

		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(Company entity) throws SQLException {
		query = "update " + table_name + " set  Code = ?, Name = ?, Myanmar_Name = ?";
		pstmt = conn.prepareStatement(query);
		int i = 1;
				pstmt.setString(i++, entity.getCode());
		pstmt.setString(i++, entity.getName());
		pstmt.setString(i++, entity.getMyanmar_Name());

		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void delete(long key) throws SQLException {
		query = getDeleteQuery();
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public Company find(long key) throws SQLException {
		Company l_Company = null;
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next())
			l_Company = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_Company;
	}

	@Override
	public ArrayList<Company> findAll() throws SQLException {
		ArrayList<Company> l_CompanyList = new ArrayList<Company>();
		query = getSelectQuery();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_CompanyList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_CompanyList;
	}

	@Override
	protected Company readRecord(ResultSet rs) throws SQLException {
		Company l_entity = new Company();
				l_entity.setCompany_Id(rs.getLong("Company_Id"));
		l_entity.setCode(rs.getString("Code"));
		l_entity.setName(rs.getString("Name"));
		l_entity.setMyanmar_Name(rs.getString("Myanmar_Name"));

		return l_entity;
	}

	public CompanyDataList getList(CriteriaParams cri) throws SQLException{
		CompanyDataList l_DataList = new CompanyDataList();
		ArrayList<Company> l_entityList = new ArrayList<Company>();
		String l_cri = "";
		query = "set rowcount " + cri.getSize() +
			" select * from (select row_number() over(order by Company_Id desc) as [rowno], * from " + table_name +
			" where 1 = 1" + l_cri + ") " + table_name +
			" where rowno > " + cri.getIndex();
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next()) {
			do {	
				l_entityList.add(readRecord(rs));
			} while (rs.next());
		}
		l_DataList.setEntityList(l_entityList);
		rs.close();
		pstmt.close();
		query = "select count(Company_Id) from " + table_name + " where 1 = 1" + l_cri;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next()){
			l_DataList.setTotalRecord(rs.getLong(1));
		}
		rs.close();
		pstmt.close();
		return l_DataList;
    }
}
